import router from '@/router'
import store from '@/store'
router.beforeEach(async(to, from, next) => {
  // 已登录
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.state.user.userInfo.username) {
        await store.dispatch('user/getUsrInfo')
        // 这里刚好拿完数据, 就应该做权限筛选, 不应该进入的页面统统去掉
        const routes = await store.dispatch('permission/fiterRoutes', store.state.user.userInfo.roles.menus)
        // // 可以通过router.addRoutes 方法来后续追加路由配置
        router.addRoutes([
          ...routes,
          { path: '*', redirect: '/404', hidden: true }
        ])
        next(to.path)
      } else {
        next()
      }
    }
  } else {
    //   未登录
    const panel = ['/login', '/404']
    if (panel.indexOf(to.path) > -1) {
      next()
    } else {
      next('/login')
    }
  }
})
